<template>
  <div class="wrap">
    <div class="handle-wrap">
      <h4>管理员列表</h4>
      <div>
        <el-button type="primary" @click="add">新增</el-button>
      </div>
    </div>
    <el-table :data="userList" style="width: 100%">
      <el-table-column prop="id" label="id"> </el-table-column>
      <el-table-column prop="name" label="名称"> </el-table-column>
      <el-table-column label="权限">
        <template v-slot="scope">
          {{ scope.row.permissions.map((permission) => permission.title).join("、") }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="250">
        <template v-slot="scope">
          <el-button
            @click.stop="editorRow(scope.row)"
            type="primary"
            size="small"
          >
            编辑
          </el-button>
          <el-button
            @click.stop="editorMenuRow(scope.row)"
            type="primary"
            size="small"
          >
            菜单权限
          </el-button>
          <el-button @click.stop="deleteRow(scope.row)" size="small">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      v-model="dialogFormVisible"
      :close-on-click-modal="false"
      :title="dialogTitle"
    >
      <el-form :model="form" label-position="top">
        <el-form-item label="名称" label-width="140px">
          <el-input v-model.trim="form.name" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="editorRoleConfirm">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>

    <el-dialog v-model="dialogTableVisible" title="菜单列表">
      <el-table
        ref="multipleTableRef"
        :data="permissionList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column property="name" label="name" width="150" />
        <el-table-column property="path" label="path" width="200" />
        <el-table-column property="title" label="title" />
      </el-table>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogTableVisible = false">取消</el-button>
          <el-button type="primary" @click="editorPermissionConfirm">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import request from "utils/request";
import { ElMessage, ElMessageBox } from "element-plus";

export default {
  name: "JddChromePluginsRole",

  data() {
    return {
      dialogTitle: "新增",
      dialogFormVisible: false,
      form: {
        id: "",
        name: "",
      },
      userList: [],
      dialogTableVisible: false,
      permissionList: [
        {
          id: "",
          name: "",
          path: "",
          component: "",
          title: "",
        },
      ],
      multipleSelection: [],
    };
  },
  watch: {
    dialogFormVisible: {
      handler(value) {
        if (!value) {
          Object.assign(this.form, {
            id: "",
            name: "",
          });
          console.log("this.form", this.form);
        }
      },
    },
    dialogTableVisible: {
      handler(value) {
        if (!value) {
          this.multipleSelection = [];
        }
      },
    },
  },
  mounted() {
    this.getRoleList();
  },

  methods: {
    async getRoleList() {
      let ret = await request.get("/getRoleList", {});
      if (ret.code == 200) {
        this.userList = ret.data || [];
      }
    },
    add() {
      this.dialogTitle = "新增";
      this.dialogFormVisible = true;
    },
    editorRow(role) {
      this.dialogTitle = "编辑";
      this.dialogFormVisible = true;
      Object.assign(this.form, role);
    },
    async deleteRow(row) {
      ElMessageBox.confirm("确认需要删除该角色?", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let ret = await request.post("/deleteRole", {
            id: row.id,
          });
          if (ret.code == 200) {
            this.$message({
              message: "移除成功",
              type: "success",
            });
            this.getRoleList();
          } else {
            this.$message({
              message: ret.message,
              type: "error",
            });
          }
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "取消删除",
          });
        });
    },
    async editorRoleConfirm() {
      let ret = await request.post("/modifyRole", {
        id: this.form.id,
        name: this.form.name,
      });

      if (ret.code == 200) {
        this.$message({
          message: "编辑成功",
          type: "success",
          showClose: true,
          duration: 1000,
        });
        this.getRoleList();
        this.dialogFormVisible = false;
      } else {
        this.$message({
          message: ret.message,
          type: "error",
          showClose: true,
          duration: 1000,
        });
      }
    },
    async getPermissionList() {
      let ret = await request.get("/getPermissionList", {});
      if (ret.code == 200) {
        this.permissionList = ret.data || [];
      }
    },
    async findRoleSelfPermissions() {
      let _this = this;
      let ret = await request.get("/findRoleSelfPermissions", {
        roleId: this.form.id,
      });
      if (ret.code == 200) {
        console.log(ret.data);
        this.permissionList
          .filter((permission) => {
            return ret.data.includes(permission.id);
          })
          .map((row) => {
            _this.$refs.multipleTableRef.toggleRowSelection(row, undefined);
          });

        console.log("this.multipleSelection", this.multipleSelection);
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    async editorMenuRow(row) {
      this.dialogTableVisible = true;
      this.form.id = row.id;
      await this.getPermissionList();
      await this.findRoleSelfPermissions();
    },
    async editorPermissionConfirm() {
      let ret = await request.post("/roleAddPermission", {
        roleId: this.form.id,
        permissionIds: this.multipleSelection.map((permission) => {
          return permission.id;
        }),
      });
      if (ret.code == 200) {
        this.$message({
          message: "授权成功",
          type: "success",
          showClose: true,
          duration: 1000,
        });
        this.getRoleList();
          this.dialogTableVisible = false;
      } else {
        this.$message({
          message: ret.message,
          type: "error",
          showClose: true,
          duration: 1000,
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.wrap {
  .handle-wrap {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .headPictureCol {
    width: 32px;
    height: 32px;
    overflow: hidden;
    border-radius: 50%;
  }
}
</style>
